package com.oktest.measure

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.cali.libcore.view.BottomBar
import com.cali.libcore.view.BottomBarTab
import com.oktest.measure.ui.fragment.HomePageFragment
import com.oktest.measure.ui.fragment.MineFragment
import com.oktest.measure.ui.fragment.WorkFragment
import com.oktest.measure.ui.fragment.work.FeedbackFragment
import me.yokeyword.fragmentation.SupportFragment


/**
 */
class MainFragment : SupportFragment(){

    private val mFragments: Array<SupportFragment?> by lazy {
        arrayOfNulls(3)
    }

    companion object {
        const val HOME_FIRST = 0
        const val HOME_WORK = 1
        const val HOME_MINE = 2
        fun newInstance(): MainFragment = MainFragment()
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val firstFragment = findChildFragment(HomePageFragment::class.java)
        if (firstFragment == null) {
            mFragments[HOME_FIRST] = HomePageFragment.newInstance()
            mFragments[HOME_WORK] = WorkFragment.newInstance()
            mFragments[HOME_MINE] = MineFragment.newInstance()
            loadMultipleRootFragment(
                R.id.fl_tab_container, HOME_FIRST,
                mFragments[HOME_FIRST],
                mFragments[HOME_WORK],
                mFragments[HOME_MINE]
            )
        } else {
            mFragments[HOME_FIRST] = firstFragment
            mFragments[HOME_WORK] = findChildFragment(WorkFragment::class.java)
            mFragments[HOME_MINE] = findChildFragment(MineFragment::class.java)
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_main, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData(view)
    }

    private fun initData(view: View) {
        val bottomBar = view.findViewById<BottomBar>(R.id.bottomBar)
        bottomBar.apply {
            addItem(BottomBarTab(context, R.drawable.selector_tab_first_page, "首页"))
            addItem(BottomBarTab(context, R.drawable.selector_tab_work, "工作"))
            addItem(BottomBarTab(context, R.drawable.selector_tab_mine, "我的"))
            setOnTabSelectedListener(object : BottomBar.OnTabSelectedListener {
                override fun  onTabSelected(position: Int, prePosition: Int):Boolean{
                    showHideFragment(mFragments[position], mFragments[prePosition])
                    return true
                }

                override fun onTabUnselected(position: Int) {
                }

                override fun onTabReselected(position: Int) {
                }
            })
        }

    }

    override fun onFragmentResult(requestCode: Int, resultCode: Int, data: Bundle?) {
        super.onFragmentResult(requestCode, resultCode, data)
        findFragment(FeedbackFragment::class.java)?.onFragmentResult(requestCode, resultCode, data)
    }
}
